<?php
require('config.php');
	$wb_id=$_REQUEST['wb_id'];
	$user_id=$_REQUEST['user_id'];
	$to_user_id=$_REQUEST['to_user_id'];
	$time=date('Y-m-d H:i:s');
	$sql="select * from ".$oto."_help_orders where helpId='{$wb_id}' and userId='{$user_id}' and acceptUserId='{$to_user_id}' and isDel='0' and orderStatus not in ('-1','-2','-5','-6')";
	$result=$db->query($sql);
	while($row=$result->fetch_assoc()){
		$isJiedan[]=$row;//将取得的所有数据赋值给person_info数组
	}
	if(isset($isJiedan))
	{
		$data[0]['pan']="-1";
		$data[0]['msg']="订单已存在";
	}
	else
	{
		$sql="select * from ".$oto."_help_order_user where help_id='{$wb_id}' and user_id='{$to_user_id}' and isDel='0'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$jiedan_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(isset($jiedan_info))
		{
			$orderStrNo=microtime(true);
			$sql="INSERT INTO ".$oto."_orderids (`rnd`) VALUES ('{$orderStrNo}')";
			$result=$db->query($sql);
			$sql="SELECT * FROM ".$oto."_orderids order by id DESC";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$orderids_info[]=$row;//将取得的所有数据赋值给person_info数组
			}
			$sql="SELECT * FROM ".$oto."_sys_configs where fieldName='佣金比例' limit 1";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$sys[]=$row;//将取得的所有数据赋值给person_info数组
			}
			$orderNo=$orderids_info[0]['id']."".(fmod($orderids_info[0]['id'],7));//订单号
			$money=$jiedan_info[0]['money'];
			$kou_yongjin=$money*($sys[0]['fieldCode']/100);
			$sql="INSERT INTO ".$oto."_help_orders (`orderNo`,`helpId`,`userId`,`orderStatus`,`acceptUserId`,`money`,`createTime`,`commission`) VALUES ('$orderNo','$wb_id','$user_id','0','$to_user_id','$money','$time','$kou_yongjin')";
			//file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
	   		$result2=$db->query($sql);
	   		$sql="select * from ".$oto."_help_orders where orderNo='{$orderNo}' and helpId='{$wb_id}' and userId='{$user_id}' and acceptUserId='{$to_user_id}' and createTime='$time'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$order_info[]=$row;//将取得的所有数据赋值给person_info数组
			}
	   		if($result2)
	   		{
	   			$data[0]['pan']="1";
				$data[0]['msg']="选择接单成功";
				$data[0]['order_id']=$order_info[0]['id'];
	   		}
	   		else
	   		{
	   			$data[0]['pan']="-1";
				$data[0]['msg']="选择接单失败";
	   		}
		}
		else
		{
			$data[0]['pan']="-1";
			$data[0]['msg']="该用户已取消接单";
		}
	}
	echo json_encode($data);
?>